# coding=iso-8859-1
'''
Created on 21.9.2011

@author: mphkh
'''
from datetime import date
from Calendar import *

class Iceland(Calendar):
    '''
    This class is a crude adaption from QuantLib Icelandic calendar implementation.
    Non-business days are:
        Saturdays
        Sundays
        New Year's Day, January 1st (possibly moved to Monday)
        Holy Thursday
        Good Friday
        Easter Monday
        First day of Summer (third or fourth Thursday in April)
        Labour Day, May 1st
        Ascension Thursday
        Pentecost Monday
        Independence Day, June 17th
        Commerce Day, first Monday in August
        Christmas, December 25th
        Boxing Day, December 26th
    '''
    def __init__(self):
        pass
    def isBusinessDay(self, ddate):
        '''
        Check if date is a business day on the Nasdaq OMX market in Iceland
        Parameters: 
            ddate:    date
        Returns:
            0 if date is a business day
            1 else
        '''
        d = ddate.day                # Day of month
        dd = ddate.timetuple()[7]    # Return timetuple index 7 (is the day number within
                                    # the current year starting with 1 for January 1st)
        w = ddate.weekday()          # Monday is 0, sunday is 6
        m = ddate.month              # January is 1, december is 12
        y = ddate.year
        em = self.easterMonday(y)
        if(self.isWeekend(ddate)
        # New Year's Day (possibly moved to Monday)
            or ((d == 1 or ((d == 2 or d == 3) and w == 0)) and m == 1)
            # Holy Thursday
            or (dd == em-4)
            # Good Friday
            or (dd == em-3)
            # Easter Monday
            or (dd == em)
            # First day of summer
            or (d >= 19 and d <= 25 and w == 4 and m == 4)
            # Ascension Thursday
            or (dd == em+38)
            # Pentecost Monday
            or (dd == em+49)
            # Labour Day
            or (d == 1 and m == 5)
            # Independence Day
            or (d == 17 and m == 6)
            # Commerce Day
            or (d <= 7 and w == 0 and m == 8)
            # Christmas
            or (d == 25 and m == 12)
            # Boxing Day
            or (d ==26 and m == 12)):
            return 0
        else:
            return 1